package com.huahua.bigdata.sparkstreaming;

import org.apache.spark.SparkConf;
import org.apache.spark.streaming.Duration;
import org.apache.spark.streaming.api.java.JavaReceiverInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;

public class SparkStreaming08_Close {
    public static void main(String[] args) throws Exception{
        SparkConf conf = new SparkConf();
        conf.setMaster("local");
        conf.setAppName("sparkStreaming");
        JavaStreamingContext jsc = new JavaStreamingContext(conf, new Duration(3 * 1000));

        JavaReceiverInputDStream<String> socketDS = jsc.socketTextStream("localhost", 9999);
        socketDS.print();

        jsc.start();
        // TODO close 方法就是用来释放资源, 关闭环境， 但是不能在当前main方法(线程)中完成
        new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO 关闭环境，释放资源
                try{
                    Thread.sleep(3000);
                    // jsc.close();  强制关闭
                    jsc.stop(true, true); // 优雅关闭
                } catch (Exception e) {

                }
            }
        }).start();

        jsc.awaitTermination();
    }
}
